package wiki.hitime.stl.daq.manager;

import wiki.hitime.stl.daq.bean.db.DaqAcquisitionDataRecord;
import wiki.hitime.stl.daq.mapper.DaqAcquisitionDataRecordMapper;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangdaoping
 * @create 2019-07-17 11:20
 */
@Service
public class DaqAcquisitionDataRecordManager {

    private DaqAcquisitionDataRecordMapper daqAcquisitionDataRecordMapper;

    public DaqAcquisitionDataRecordManager(DaqAcquisitionDataRecordMapper daqAcquisitionDataRecordMapper) {
        this.daqAcquisitionDataRecordMapper = daqAcquisitionDataRecordMapper;
    }

    /**
     * 添加数据采集数据
     *
     * @param acquisitionRecordId 数据采集记录编号
     * @param dataRecord          采集数据集合
     */
    public void addDataRecord(Long acquisitionRecordId, List<Object[]> dataRecord) {
        List<DaqAcquisitionDataRecord> acquisitionDataRecordList = new ArrayList<>(dataRecord.size());
        for (Object[] dataArrays : dataRecord) {
            DaqAcquisitionDataRecord acquisitionDataRecord = DaqAcquisitionDataRecord.builder()
                    .acquisitionRecordId(acquisitionRecordId)
                    .dataA(dataArrays[0].toString())
                    .build();
            acquisitionDataRecordList.add(acquisitionDataRecord);
        }
        daqAcquisitionDataRecordMapper.batchAdd(acquisitionDataRecordList);
    }

    /**
     * 加载采集的数据记录
     *
     * @param acquisitionRecordId 采集记录编号
     * @return 采集的数据
     */
    public List<String[]> loadDataRecord(Long acquisitionRecordId) {
        Example selectExample = new Example(DaqAcquisitionDataRecord.class, true, true);
        selectExample.createCriteria().andEqualTo("acquisitionRecordId", acquisitionRecordId).andEqualTo("isDelete", Boolean.FALSE);
        List<DaqAcquisitionDataRecord> dataRecordList = daqAcquisitionDataRecordMapper.selectByExample(selectExample);
        List<String[]> dataList = new ArrayList<>(dataRecordList.size());
        for (DaqAcquisitionDataRecord record : dataRecordList) {
            dataList.add(new String[]{record.getDataA()});
        }
        return dataList;
    }
}
